package com.sky.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.aop.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

   @Override
    public PageResult list(DishPageQueryDTO dishDTO) {

// ===========================自己写的方法=========================================
//        // 设置分页参数
//        PageHelper.startPage(dishDTO.getPage(),dishDTO.getPageSize());
//        // 执行分页查询
//        List<DishVO> empList = dishMapper.list(dishDTO);
//       for (DishVO s : empList) {
//            Long id = s.getId();
//            Integer categoryId = dishMapper.selectById(id).getCategoryId();
//            Long f = Long.valueOf(categoryId);
//            String name = categoryMapper.selectById(f).getName();
//            s.setCategoryName(name);
//        }
//        // 获取分页结果
//        Page<DishVO> p = (Page<DishVO>) empList;
//        //封装PageBean
//        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
//        return pageResult;

// ===========================方法 2 =========================================
       PageHelper.startPage(dishDTO.getPage(), dishDTO.getPageSize());
       Page<DishVO> page = (Page<DishVO>) dishMapper.list(dishDTO);
       // 获取查询到的菜品信息
       List<DishVO> result = page.getResult();
       // 获取菜品信息中所有的分类id
       List<Long> categoryIdList = result.stream().map(DishVO::getCategoryId).collect(Collectors.toList());
       Set<Long> categoryIds = new HashSet<>(categoryIdList);
       // 通过多个分类id查询相应的分类信息
       List<Category> categoryList = categoryMapper.findByIds(categoryIds);
       // 获取分类id对应的分类名称
       Map<Long, String> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, Category::getName));
       result.stream().forEach(dishVO -> {
           String categoryName = categoryMap.get(dishVO.getCategoryId());
           dishVO.setCategoryName(categoryName);
       });
       return new PageResult(page.getTotal(), result);

    }

//    修改菜品
    @Transactional
    public void updateDish(DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        log.info("修改菜品 dish{}",dish.toString());

        //根据id 查询 category_id
        Dish dish1 = dishMapper.selectById(dishDTO.getId());
        Long categoryId = dish1.getCategoryId();
        dish.setCategoryId(categoryId);
        //修改 dish
        dishMapper.updateById(dish);

        //修改 口味
        for (DishFlavor flavor : dishDTO.getFlavors()) {
            //先删除dish_flavor中的对应数据
            dishFlavorMapper.deletById(flavor.getDishid());
        }
        for (DishFlavor flavor : dishDTO.getFlavors()) {
            //在添加口味
            flavor.setDishid(dishDTO.getId());
            dishFlavorMapper.saveDishFlavor(flavor);
        }
    }

//    批量删除菜品
    @Transactional
    public void removeById(Long[] ids){
        //删除 菜品
        for (Long id : ids) {
            log.info("当前删除的菜品 ID ：{}",id);
            dishMapper.removeById(id);
        }
        //s删除 口味
        for (Long id : ids) {
            log.info("当前删除的菜品 ID ：{}",id);
            dishFlavorMapper.deletById(id);
        }
    }

//    新增菜品
    @Transactional
    public void save(DishDTO dishDTO) {
        log.info(dishDTO.toString());
        dishMapper.SaveDish(dishDTO);

        Long dishId = dishDTO.getId();   //菜品id
        log.info("dishId:{}",dishId.toString());

        List<DishFlavor> flavors = dishDTO.getFlavors();

        //保存菜品口味数据到菜品口味表dish_flavor
        for (DishFlavor flavor : flavors) {
            flavor.setDishid(dishId);
            dishFlavorMapper.saveDishFlavor(flavor);
        }
    }


//    根据id查询菜品
    @Override
    public DishVO selectById(Long id) {
        //获取Dish表数据
        Dish dish = dishMapper.selectById(id);
        log.info("根据id查询菜品,{}",dish.toString());
        //获取口味表数据

        List<DishFlavor> dishFlavor = dishFlavorMapper.getDishFlavor(id);
        log.info("口味数据:{}",dishFlavor.toString());

        //获取
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);

        dishVO.setFlavors(dishFlavor);
        log.info("根据id查询菜品,dishVO:{}",dishVO);

        return dishVO;
    }


    //    根据分类id查询菜品
    @Override
    public List<Dish> selectByCateId(Long categoryId){
        List<Dish> dish = dishMapper.selectByCateId(categoryId);
        return dish;
    }


    //    菜品起售、停售
    @Override
    public Integer updateStatus(Long id, Integer status) {
        Integer integer = dishMapper.updateStatus(id, status);

        return integer;
    }
}
